package com.work.csms.controller;

import com.work.csms.dao.Notice;
import com.work.csms.pojo.PageParam;
import com.work.csms.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/notice")
@CrossOrigin(originPatterns = "*",allowCredentials ="true")
public class NoticeController {

    @Autowired(required = false)
    private NoticeService noticeService;

    @PostMapping("/page")
    public Map<String, Object> page(@RequestBody Notice notice, @RequestBody PageParam pageParam) throws Exception {
        return noticeService.page(notice, pageParam);
    }

    @PostMapping("/list")
    public List<Notice> list(@RequestBody Notice notice) throws Exception {
        return noticeService.list(notice);
    }

    @RequestMapping("/getNoticeById")
    public Notice getNoticeById(@RequestBody Notice notice) throws Exception {
        Integer id = notice.getId();
        if(id == null) {
            return null;
        } else {
            return noticeService.getNoticeById(id);
        }
    }

    @RequestMapping("/saveOrUpdate")
    public Map<String, Object> saveOrUpdate(@RequestBody Notice notice) throws Exception {
        Map<String, Object> result = new HashMap<>();
        Integer id = notice.getId();
        if (Objects.isNull(id)) {
            noticeService.save(notice);
        } else {
            noticeService.update(notice);
        }
        result.put("success", true);
        return result;
    }

    @RequestMapping("/deleted")
    public Map<String, Object> deleted(@RequestBody Notice notice) throws Exception {
        Map<String, Object> result = new HashMap<>();
        Integer id = notice.getId();
        if (Objects.isNull(id)) {
            result.put("msg", "公告id不存在");
        } else {
            noticeService.deleted(notice.getId());
            result.put("success", true);
        }
        return result;
    }
}
